package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.entity.IndentOrder;
import cn.stylefeng.guns.modular.system.mapper.IndentOrderMapper;
import cn.stylefeng.guns.modular.system.mapper.PurchaseOrdersMapper;
import cn.stylefeng.guns.modular.system.model.params.IndentOrderParam;
import cn.stylefeng.guns.modular.system.service.DealerLevelService;
import cn.stylefeng.guns.modular.system.service.IndentOrderService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 订货单控制器
 *
 * @author 小飞
 * @Date 2019-07-02 16:39:03
 */
@Controller
@RequestMapping("/indentOrder")
public class IndentOrderController extends BaseController {

    private String PREFIX = "/indentOrder";

    @Autowired
    private IndentOrderService indentOrderService;
    @Autowired
    private DeptService  deptService;
    @Autowired
    private DealerLevelService dealerLevelService;
    @Autowired
    private PurchaseOrdersMapper purchaseOrdersMapper;
    @Autowired
    private IndentOrderMapper indentOrderMapper;
    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @RequestMapping("")
    public String index(Model model) {

        Long usreId = ShiroKit.getUserNotNull().getId();
        model.addAttribute("id",usreId);
        return PREFIX + "/indentOrder.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @RequestMapping("/add")
    public String add(Model model) {
        getUserDetail(model);
        return PREFIX + "/indentOrder_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @RequestMapping("/edit")
    public String edit(Long id,Model model) {
        getUserDetail(model);
        model.addAttribute("id",id);
        IndentOrder indentOrder = indentOrderService.getById(id);
        model.addAttribute("dealer",indentOrder.getDealer());
        model.addAttribute("ids",indentOrder.getPurchaseId());
        return PREFIX + "/indentOrder_edit.html";
    }


    /**
     * 发货
     * @return
     */
    @RequestMapping("/send")
    public String send(@RequestParam("id") Long id){
        super.setAttr("id",id);
        super.setAttr("username",ShiroKit.getUserNotNull().getName());
        super.setAttr("deptName",deptService.getById(ShiroKit.getUserNotNull().getDeptId()).getSimpleName());
        return  PREFIX + "/indentOrder_send.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(IndentOrderParam indentOrderParam) {

        this.indentOrderService.add(indentOrderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(IndentOrderParam indentOrderParam) {
        this.indentOrderService.delete(indentOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(IndentOrderParam indentOrderParam) {
        IndentOrder detail = this.indentOrderService.getById(indentOrderParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-07-02
     */
    @ResponseBody
    @RequestMapping("/list/{id}")
    public LayuiPageInfo list(IndentOrderParam indentOrderParam,@PathVariable  Long id) {
        return this.indentOrderService.findPageBySpec(indentOrderParam,id);
    }


    /**
     * 得到用户详细信息
     * @param model
     */
   public void  getUserDetail(Model model){

       //经销商登录名
       ShiroUser shiroUser =  ShiroKit.getUserNotNull();
       model.addAttribute("username",shiroUser.getName());

       //部门
       String deptName = deptService.getById(shiroUser.getDeptId()).getSimpleName();
       model.addAttribute("deptName",deptName);

       /*//经销商名称
       String dealerName = dealerLevelService.getOne(new QueryWrapper<DealerLevel>()
               .eq("sys_user_id",shiroUser. getId())).getName();
       model.addAttribute("dealerName",dealerName);*/

   }

    /**
     * 获取该经销商的订单
     * @return
     */
    @ResponseBody
   @RequestMapping("/listByDealer")
   public LayuiPageInfo listByDealer(){
       Page page = LayuiPageFactory.defaultPage();
       IPage pageUse = indentOrderMapper.getIndentByDealer(page,ShiroKit.getUserNotNull().getId());
       return  LayuiPageFactory.createPageInfo(pageUse);
   }





}


